import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { requestUser } from '@/api/user'
import { Toast } from 'vant'
import { useUserStore } from '@/stores/user'
interface values{
    username:string,
    password:string,
}
/**
 * 登录业务
 */
 export const useMemberLogin = () => {
	const store = useUserStore()
	const router = useRouter()
	const username = ref('')
	const password = ref('')
	const onSubmit = async (values:values) => {
		let data = await requestUser(values.username, values.password)
		let { resultCode, resultInfo, token } = data
		if (resultCode == 1) {
			// 1.保存用户信息
			store.saveUser({ nick: resultInfo.nick, headerimg: resultInfo.headerimg })
			// 2.保存token
			localStorage.setItem('TOKEN', token)
			// 3.跳转到首页
			router.replace({path:'/home'})
		} else {
			Toast.fail('用户名或密码出错!')
		}
	}
	const onClickLeft = () => {
		history.back()
	}

	return {
		onSubmit,
		onClickLeft,
		username,
		password,
	}
}
export const useMemberRegister=()=>{
	const router = useRouter()

    const toRegist=()=>{
        router.replace({path:'/register'})

    }
    return{
        toRegist,
    }
}
